import 'package:flutter/material.dart';
import 'package:woniutaofang/components/ui/custom_icon.dart';
import 'package:woniutaofang/components/ui/dialog.dart';
import 'package:woniutaofang/model/Home/getHomeBuildings.dart';
import 'package:woniutaofang/pages/home/Property/components/swiper_widget.dart';
import 'package:woniutaofang/service/api_service.dart';
import 'package:woniutaofang/utils/utils.dart';

class PropertyPage extends StatefulWidget {
  final String id;
  PropertyPage(this.id);
  @override
  _PropertyPageState createState() => _PropertyPageState();
}

class _PropertyPageState extends State<PropertyPage>
    with TickerProviderStateMixin {
  List<String> pics = [
    'https://wx2.sinaimg.cn/mw690/66fa6be4gy1fzd1sif9tuj218g0p143d.jpg',
    'https://wx2.sinaimg.cn/mw690/66fa6be4gy1fzd1sif9tuj218g0p143d.jpg',
    'https://wx2.sinaimg.cn/mw690/66fa6be4gy1fzd1sif9tuj218g0p143d.jpg',
  ];
  List goods;

  double navAlpha = 0;
  double headerHeight;
  ScrollController scrollController = ScrollController();

  TabController controller;
  List<Tab> titleTabs;

  int currentIndex = 0;

  Color c = Colors.grey;

  @override
  void initState() {
    super.initState();

    titleTabs = <Tab>[Tab(text: '详情'), Tab(text: "评论")];

    controller = TabController(length: titleTabs.length, vsync: this)
      ..addListener(() {
        // 监听滑动/点选位置
        if (controller.index.toDouble() == controller.animation.value) {
          setState(() => currentIndex = controller.index);
        }
      });

    headerHeight = Utils.width;

    scrollController.addListener(() {
      var offset = scrollController.offset;
      if (offset <= 0) {
        if (navAlpha != 0) {
          setState(() => navAlpha = 0);
        }
      } else if (offset < headerHeight) {
        if (headerHeight - offset <= Utils.navigationBarHeight) {
          setState(() => c = Colors.white);
        } else {
          c = Colors.grey;
        }
        setState(() => navAlpha = 1 - (headerHeight - offset) / headerHeight);
      } else if (navAlpha != 1) {
        setState(() => navAlpha = 1);
      }
    });

    _getHomeBuildings();
  }

  void _getHomeBuildings() async {
    GetHomeBuildingsModel data = await ApiService.getHomeBuildings();
    if (data != null) {
      setState(() {
        goods = data.data;
      });
      // state = LoaderState.Succeed;
    } else {
      // state = LoaderState.NoAction;
    }
  }

  @override
  void dispose() {
    scrollController.dispose();
    // controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    print(widget.id);
    return Scaffold(body: _buildBodyView());
  }

  Widget _buildBodyView() {
    return goods == null
        ? LoadingWidget()
        : CustomScrollView(
            controller: scrollController,
            slivers: <Widget>[
              /// 头部banner
              _buildSliverAppBar(pics),
              // _buildSliverToBoxAdapter(),
              // _buildSliverList(context),
            ],
          );
  }

  Widget _buildSliverAppBar(List<String> pics) {
    return SliverAppBar(
      title: Text(
        '楼盘详情',
        style: TextStyle(
          color: Color.fromARGB((navAlpha * 255).toInt(), 255, 255, 255),
        ),
      ),
      pinned: true,
      expandedHeight: 300,
      leading: IconButton(
        icon: Icon(CustomIcon.back, color: c),
        onPressed: () => Navigator.pop(context),
      ),
      flexibleSpace:
          FlexibleSpaceBar(background: SwiperHeroWidget(pics, widget.id)),
    );
  }

  Widget _buildSliverToBoxAdapter() {
    /// SliverToBoxAdapter的child属性可以加载普通widget组件
    return SliverToBoxAdapter(
      child: Container(
        alignment: Alignment.center,
        child: Text(
          'SliverToBoxAdapter',
          style: TextStyle(color: Colors.white, fontSize: 24.0),
        ),
        height: 100.0,
        color: Colors.pinkAccent.withOpacity(0.8),
      ),
    );
  }

  Widget _buildSliverList(BuildContext context) {
    /// SliverList类似ListView，他有两种表现形式：SliverChildBuilderDelegate和SliverChildListDelegate，两者区别在于SliverChildBuilderDelegate可以加载不确定数量的列表，而SliverChildListDelegate只能加载固定已知数量。
    return SliverList(
      delegate: SliverChildBuilderDelegate(
        (context, index) {
          return Container(
            height: 40.0,
            child: Text('Sliver List Item $index'),
            alignment: Alignment.center,
            color: Colors.red[100 * (index % 10)],
          );
        },
        childCount: 10,
      ),
    );
  }
}
